<?php

class SiteController extends Controller {

    //public $layout = false;

    public function actionIndex() {
        if (user()->isGuest)
            $this->redirect(array('login'));
        $this->renderPartial('index');
    }

    // 后台登录
    public function actionLogin() {
        if (!user()->isGuest)
            $this->redirect(array('index'));

        $model = new LoginForm;
        if (isset($_POST['LoginForm'])) {
            $model->attributes = $_POST['LoginForm'];
            if ($model->validate() && $model->login())
                $this->redirect(array('index'));
        }
        $this->renderPartial('login', array('model' => $model));
    }

    // 登出
    public function actionLogout() {
        user()->logout();
        $this->redirect(array('login'));
    }

    // 后台顶部
    public function actionTop() {
        $this->layout = false;
        $this->render('top');
    }

    // 后台工具条
    public function actionSidebar() {
        cs()->registerCoreScript('jquery');
        $this->layout = false;
        $this->render('sidebar');
    }

    // 后台主页面
    public function actionMain() {
        $this->render('main');
    }

    // 网站设置
    public function actionSetting() {
        $model = new OptionsForm;
        if (isset($_POST['OptionsForm'])) {
            $content = "<?php\r\n\r\n return " . var_export($_POST['OptionsForm'], true) . "\r\n\r\n?>";
            @file_put_contents(app()->basePath . '/config/params.php', $content);
            $this->redirect('options');
        }
        $this->render('setting', array('model' => $model));
    }

}